﻿function getcallermsg(){
	var url="/tools/ajax.aspx?type=getcallermsg";
	var xmlHttp=GetXmlHttpObject();
	xmlHttp.open("GET",encodeURI(url),true);
	xmlHttp.setRequestHeader("If-Modified-Since", "0");
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
				if(xmlHttp.status==200){
				   //var html='<input type="button" value="OK" />';
				    var div=getId("callermsgDiv");
				    if(div==null){
				        div=document.createElement("div");
				        div.id="callermsgDiv";
				        div.className="messagebox";
				        document.getElementsByTagName("body")[0].appendChild(div);
				    }
				    if(xmlHttp.responseText.length==0){
				        div.parentNode.removeChild(div);				       
				    }
				    else{
				        div.innerHTML=xmlHttp.responseText;
				        getId("callerClose").onclick=function(){div.parentNode.removeChild(div);}
				    }
				    setTimeout(getcallermsg,60*1000);
				    window.onscroll();	    
				}
			}
	};
	xmlHttp.send(null);
}
function GetXmlHttpObject(){
  var xmlHttp=null;
  try{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
  catch (e){
	// Internet Explorer
	try{
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	catch (e){
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
  return xmlHttp;
}
var tmpWindowScroll100602=window.onscroll;
window.onscroll = function() {
    if(typeof(tmpWindowScroll100602)=="function") tmpWindowScroll100602();
    var content = getId("callermsgDiv");
    if(content==null) return;
    var offsetTop = document.documentElement.clientHeight - 195;
    var offsetLeft = document.documentElement.clientWidth - 265;
    content.style.top = document.documentElement.scrollTop + offsetTop + "px";
    content.style.left = document.documentElement.scrollLeft + offsetLeft + "px";
    if (navigator.userAgent.indexOf('Safari') != -1) {
       if (window.scrollY == 0) {
            content.style.top = window.scrollY + offsetTop + "px";
            content.style.left = window.scrollX + offsetLeft + "px";
        }
        else {
            content.style.top =window.scrollY +  offsetTop + 60 + "px";
            content.style.left = window.scrollX + offsetLeft + "px";
        }
    }
}
var tmpWindowLoad100602=window.onload
window.onload=function(){
    if(typeof(tmpWindowLoad100602)=="function") tmpWindowLoad100602();
    getcallermsg();
}

function checkFans(uid){
    var div=document.getElementById("callermsgDiv");
    if(div) div.parentNode.removeChild(div);
    var url="/tools/ajax.aspx?type=checkfans&fansuid=" + uid;
	var xmlHttp=GetXmlHttpObject();
	xmlHttp.open("GET",encodeURI(url),true);
	xmlHttp.setRequestHeader("If-Modified-Since", "0");
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){		        
				if(xmlHttp.status==200){
				   if(xmlHttp.responseText=="issame"){
				        msgBox("不能抢自己！");
				   }
				   else if(xmlHttp.responseText.split(",")[0]=="robbed"){
				        var user=xmlHttp.responseText.split(",");
				        msgBox('Sorry你慢了一步，已经被[<a href="/user/' + user[1] + '.aspx" >' + user[2] + '</a>]捷足先登了！');    
				   }
				   else if(xmlHttp.responseText.split(",")[0]=="ok"){
				        var user=xmlHttp.responseText.split(",");
				        //document.body.appendChild(createSendBox(user[1],user[2]));
				        msgBox('恭喜你，抢到[<a href="/user/' + user[1] + '.aspx" >' + user[2] + '</a>]成为你的粉丝！');
				        
				   }
				   else if(xmlHttp.responseText=="err"){
				        msgBox("程序出错，请稍后再试！");
				   }
				}
			}
	};
	xmlHttp.send(null);
}
function removeSendMsgBox(){
    document.body.removeChild(document.getElementById("sendMsgBox"));
}
function submitSendMsg(uid){
    var msg = getId("msgContent").value;
    if (msg.length == 0) {
        msgBox("消息不能为空！");
        return;
    }
    if (msg.length > 140) {
        msgBox("消息过长，请进行删减！")
        return;
    }
    var _url = encodeURI("/tools/ajax.aspx?type=robbedfans&userID=" + uid + "&msg=" + msg);
    
    var xmlHttp=GetXmlHttpObject();
	xmlHttp.open("GET",encodeURI(_url),true);
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
				if(xmlHttp.status==200){
				    var data=xmlHttp.responseText;
				    if (data == "true") {
				        msgBox("恭喜你，多了一位粉丝！");
                        removeSendMsgBox();
                    }
                    else {
                        msgBox("表白失败！");
                    }  
				}
			}
	};
	xmlHttp.send(null);
}
function msgContentChange(target){
    if(target.value.length>200){
        target.value=target.value.substr(0,200);
    }
}
function createSendBox(uid,uname){
    var sendMsgBox=document.createElement("div");
    sendMsgBox.id="sendMsgBox";
    var top=document.documentElement.scrollTop + 70;
	var left=document.documentElement.scrollLeft + 2;
	if (navigator.userAgent.indexOf('Safari') != -1) {
			top = window.scrollY + 70;
			left = window.scrollX + 2;
	}
    sendMsgBox.style.cssText="z-index: 99;top:" + top + "px;left:" + left + "px;";
    sendMsgBox.className="comment"
    sendMsgBox.innerHTML='<div style="height: 28px; line-height: 28px;">\
                <span>发送消息</span>\
                <input type="button" class="closebtn" onclick="removeSendMsgBox()" value=""></div>\
            <div style="height: 30px; line-height: 30px; padding-left: 10px;">\
                <p>抢到了，只需一句问候对方就成为你粉丝！</p>\
            </div>\
            <div class="edit_c">\
                <textarea id="msgContent" onkeyup="msgContentChange(this)" name="textarea"></textarea></div>\
            <div class="button">\
                <span id="Spandsg"></span>\
                <input type="button" onclick="removeSendMsgBox()" value="取&nbsp;&nbsp;消">\
                <input type="button" onclick="submitSendMsg(' + uid + ')" value="确认发送" style="background-image: url(&quot;/images/comment/l00402_07.png&quot;); float: right; margin-left: 0px;">\
            </div>'
   return sendMsgBox;
}
